package com.example.baselibrary.base

import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import com.example.baselibrary.R
import com.gyf.immersionbar.ImmersionBar
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe

abstract class BaseVmActivity<VB : ViewBinding, VM : BaseViewModel> : BaseActivity<VB>() {

    protected open lateinit var mViewModel: VM

    //分页参数
    protected open val mTotalCount = 20//每次加载数量
    protected open var mCurrentSize = 0//当前加载数量
    protected open var mCurrentPage = 0//当前加载页数

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
        ImmersionBar.with(this)  .fullScreen(false) .statusBarDarkFont(statusBarDark()).keyboardEnable(keyboardImmersionBarEnable()).navigationBarColor(
            R.color.white).init()
        initViewModel()
        observe()
        initView()
        initData()
        setListener()
    }

    fun onBack(view: View) {
        finish()
    }
    /**
     * 状态栏颜色
     */
     override fun statusBarDark(): Boolean {
        return true
    }
    /**
     * 状态栏与输入框是否兼容
     */
    protected open fun keyboardImmersionBarEnable(): Boolean {
        return true
    }
    /**
     * 初始化ViewModel
     */
    private fun initViewModel() {
        mViewModel = ViewModelProvider(this).get(viewModelClass())
    }

    /**
     * 获取ViewModel的class
     */
    protected abstract fun viewModelClass(): Class<VM>

    /**
     * 订阅，有逻辑的话，复写的时候super不要去掉
     */
    open fun observe() {

    }

    /**
     * 初始化view相关
     */
    open fun initView() {}

    /**
     * 初始化data相关
     */
    open fun initData() {}

    /**
     * 设置监听
     */
    open fun setListener() {}

    @Subscribe
    open fun fragmenEvent(event: Any?) {

    }

    override fun onDestroy() {
        EventBus.getDefault().unregister(this)
        super.onDestroy()
        mCurrentSize = 0
        mCurrentPage = 0
    }
}
